#include<vector>

using namespace std;

class Solution {
public:
    int trap(vector<int>& height) {
        int maxdes = 0;
        int obs = 0;
        int prev = 0;
        int des = 0;
        int count = 0;
        while (prev < height.size() - 1)
        {
            if (height[prev] == 0)
            {
                prev++;
                continue;
            }
            if (des <= prev || height[des] < height[prev])
            {
                des++;
                if(des<height.size())
                maxdes = height[des] > height[maxdes] ? des : maxdes;
                continue;
            }
            if (des == height.size())
            {
                des = maxdes;
            }
            for (int i = prev + 1; i < des; i++)
            {
                obs += height[i];
            }
            count += (des - prev-1) * (height[prev] < height[des] ? height[prev] : height[des]) - obs;
            prev = des;
        }
        return count;
    }
};

int main()
{
    Solution s;
    vector<int> a = { 0,1,0,2,1,0,1,3,2,1,2,1 };
    s.trap(a);
    return 0;
}